//
//  YTURLDispatcher.m
//  Pods
//
//  Created by wenzhan on 15/11/17.
//
//

#import "YTURLDispatcher.h"
#import "YTLocationManager.h"
#import "YTPayManager.h"
#import "YTShareManager.h"
#import "YTPayManager.h"
#import "WeiboSDK.h"
#import "WXApi.h"

#import "YTLogManager.h"

static NSString *const YTURLDispatcherWeixinAppIDKey = @"YTURLDispatcherWeixinAppId";
static NSString *const YTURLDispatcherSinaWeiboAppIDKey = @"YTURLDispatcherSinaWeiboAppId";
static NSString *const YTURLDispatcherAPPSchemaKey = @"YTURLDispatcherAppSchema";

@interface YTURLDispatcher ()

+ (NSURL*)formateURLWithString;

+ (void)setupWeixinDispatcherWithTradeNo:(NSString*)tradeNo
                                prepayId:(NSString*)prepayId
                                nonceStr:(NSString*)nonceStr
                               timeStamp:(NSString*)timeStamp
                            packageValue:(NSString*)packageValue
                                    sign:(NSString*)sign
                             resultBlock:(YTPayResultBlock)resultBlock;

@end

@implementation YTURLDispatcher

#pragma mark - temporary test

// URL for rootViewController
+ (NSURL*)formateURLWithString
{
    NSString* url = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleWebURLString"];   //Define in info.plist
    NSURL* URL = [NSURL URLWithString:url];
    return URL;
}

//setup for weixin pay
+ (void)setupWeixinDispatcherWithTradeNo:(NSString*)tradeNo
                                prepayId:(NSString*)prepayId
                                nonceStr:(NSString*)nonceStr
                               timeStamp:(NSString*)timeStamp
                            packageValue:(NSString*)packageValue
                                    sign:(NSString*)sign
                             resultBlock:(YTPayResultBlock)resultBlock
{
    NSString *appId = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleWeixinAppId"];
    NSString *partnerId = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleWeixinPartnerId"];
    [[YTPayManager sharedInstance]payWeixinWithTradeNo:tradeNo
                                                 appId:appId
                                             partnerId:partnerId
                                              prepayId:prepayId
                                              nonceStr:nonceStr
                                             timeStamp:timeStamp
                                          packageValue:packageValue
                                                  sign:sign
                                           resultBlock:resultBlock];
}


#pragma mark - life cycle

- (instancetype)init {
    self = [super init];
    if (self) {
        _weixinAppID = [self p_checkInfoDictionaryForKey:YTURLDispatcherWeixinAppIDKey];
        _sinaWeiboAppID = [self p_checkInfoDictionaryForKey:YTURLDispatcherSinaWeiboAppIDKey];
        _appSchema = [self p_checkInfoDictionaryForKey:YTURLDispatcherAPPSchemaKey];
        
        if (_weixinAppID) {
            [WXApi registerApp:_weixinAppID];
        }
        
        if (_sinaWeiboAppID) {
            [WeiboSDK registerApp:_sinaWeiboAppID];
        }
    }
    return self;
}

#pragma mark - delegate methods

#pragma mark - event response

#pragma mark - private methods

- (id)p_checkInfoDictionaryForKey:(NSString *)key {
    id object = [[NSBundle mainBundle] objectForInfoDictionaryKey:key];
    if (object) {
        YTLogInfo(@"分发 %@ 已经在工程文件中配置, 为 %@", key, object);
    } else {
        YTLogError(@"分发 %@ 没有在工程文件中配置, 你需要手动配置", key);
    }
    return object;
}

#pragma mark - accessor methods

- (void)setWeixinAppID:(NSString *)weixinAppID {
    _weixinAppID = weixinAppID;
    if (_weixinAppID) {
        [WXApi registerApp:_weixinAppID];
    } else {
        YTLogError(@"分发 微信 appid 为空");
    }
}

- (void)setSinaWeiboAppID:(NSString *)sinaWeiboAppID {
    _sinaWeiboAppID = sinaWeiboAppID;
    if (_sinaWeiboAppID) {
        [WeiboSDK registerApp:_weixinAppID];
    } else {
        YTLogError(@"分发 新浪微博 appid 为空");
    }
}

- (void)setAppSchema:(NSString *)appSchema {
    _appSchema = appSchema;
    if (_appSchema) {
    } else {
        YTLogError(@"分发 应用 app schema 为空");
    }
}

#pragma mark - api methods

static YTURLDispatcher *shareInstance = nil;

+ (instancetype)sharedInstance {
    static dispatch_once_t YTURLDispatcherOnceToken;
    dispatch_once(&YTURLDispatcherOnceToken, ^{
        shareInstance = [[[self class] alloc] init];
    });
    return shareInstance;
}

- (BOOL)handleURL:(NSURL *)URL {
    if (([URL.scheme isEqualToString:self.weixinAppID] && [URL.host isEqualToString:@"pay"])
        || ([URL.scheme isEqualToString:self.appSchema] && [URL.host isEqualToString:@"safepay"])) {
        return [[YTPayManager sharedInstance] handleOpenURL:URL];
    } else if ([URL.scheme isEqualToString:self.weixinAppID]
               || [URL.scheme isEqualToString:[NSString stringWithFormat:@"wb%@", self.sinaWeiboAppID]]) {
        return [[YTShareManager sharedInstance] handleOpenURL:URL];
    }
    return YES;
}

@end
